﻿// **********************************************************************
//
// Author: KingSley
// Email:3268374537@qq.com
// Created:	2018/8/1   11:45
// Introduce: 技能攻击范围判定
//
// **********************************************************************
using UnityEngine;

namespace KingSley
{
    public class SkillRange : MonoSingleton<SkillRange>
    {
        /// <summary>
        /// 扇形范围判定
        /// </summary>
        /// <param name="self">技能释放者</param>
        /// <param name="enemy">目标对象</param>
        /// <param name="maxDiatance">最大范围</param>
        /// <param name="maxAngle">最大角度</param>
        /// <returns><c>true</c>，在范围内，<c>false</c>其他情况。</returns>
        public static bool CurveRange(Transform self, Transform enemy, float maxDiatance, float maxAngle)
        {
            Vector3 playerDir = self.forward;
            Vector3 enemydir = (enemy.position - self.position).normalized;
            float angle = Vector3.Angle(playerDir, enemydir);
            float distance = Vector3.Distance(enemy.position, self.position);
            if (angle <= maxAngle / 2 && distance <= maxDiatance)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 圆形范围判定
        /// </summary>
        /// <param name="self">技能释放者</param>
        /// <param name="enemy">目标对象</param>
        /// <param name="maxDiatance">最大范围</param>
        /// <returns><c>true</c>，在范围内，<c>false</c>其他情况。</returns>
        public static bool CurveRange(Transform self, Transform enemy, float maxDiatance)
        {
            float distance = Vector3.Distance(enemy.position, self.position);
            if (distance <= maxDiatance)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 矩形范围判定
        /// </summary>
        /// <param name="self">技能释放者</param>
        /// <param name="enemy">目标对象</param>
        /// <param name="maxWidth">最大宽度</param>
        /// <param name="maxHeight">最大长度</param>
        /// <returns><c>true</c>，在范围内，<c>false</c>其他情况。</returns>
        public static bool SquareRange(Transform self, Transform enemy, float maxWidth, float maxHeight)
        {
            Vector3 enemyDir = enemy.position - self.position;
            float distance = Vector3.Distance(enemy.position, self.position);

            float angle = Vector3.Angle(enemyDir, self.forward);
            if (angle <= 90)
            {
                float z = distance * Mathf.Cos(angle * Mathf.Deg2Rad);
                float x = distance * Mathf.Sin(angle * Mathf.Deg2Rad);
                if (x <= maxWidth / 2 && z <= maxHeight)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
